#!/bin/bash

printUsage() {
	echo "Usage: $0 <full class name>"
	echo
	echo -e "\tExample: $0 java.lang.Object"
	echo
	echo -e "\tPrerequisite:"
	echo -e "\t\t1. root privilage for searching whole file system"
	echo -e "\t\t2. comamand 'jar' for listing content of jar file"
	echo
	echo "See https://code.google.com/p/locate-jars-by-full-class-name-in-whole-file-system/ for more details."
}

if [ "$1" = "" ]; then
	printUsage
	exit 1
else
	FQDN=$1
	FQDN2=$(echo $1 | sed 's/\./\//g')
fi

if [ "$(which jar)" = "" ]; then
	echo "Can't find the comand 'jar' in following paths: $PATH"

	printUsage
	exit 2
fi

echo "Start searching, you can use the command 'tail -f /tmp/locate-jar.log' to monitoring the progress"
for JAR in $(find / -name "*.jar")
do
	echo $JAR >> /tmp/locate-jar.log

	SEARCH_RESULT=$(jar -tf $JAR $FQDN2 2> /dev/null)
	[ ! "$SEARCH_RESULT" = "" ] && echo "$FQDN found in $JAR"
done
